package com.bainuo.doctor.common.image_support.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

import com.bainuo.doctor.common.R;

/**
 * Created by bainuo on 2018/3/22.
 */

@SuppressLint("AppCompatCustomView")
public class BNSwitchView extends ImageView{

    private OnClickListener listener;
    private boolean on;

    public BNSwitchView(Context context) {
        super(context);
        init();
    }

    public BNSwitchView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public void init(){
        setOn(on);
        setClickable(true);
        super.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                on = !on;
                setOn(on);
                if (listener != null)
                    listener.onClick(v);
            }
        });
    }


    public boolean isOn(){
        return on;
    }

    public void setOn(boolean on) {
        this.on = on;
        setImageResource(on? R.mipmap.icon_off:R.mipmap.icon_open);
    }

    @Override
    public void setOnClickListener(@Nullable OnClickListener l) {
        listener = l;
    }
}
